<template>
  <div class="home-container">
    <!-- 导航栏 -->
    <van-nav-bar class="page-nav-bar" fixed>
      <van-button
        class="search-btn"
        slot="title"
        type="info"
        size="small"
        round
        icon="search"
        to="/search"
        >搜索</van-button
      >
    </van-nav-bar>
    <!-- /导航栏 -->
    <!-- 频道列表
    组件属性说明：
    通过v-model绑定当前激活标签对应的索引值，默认情况下启用第一个标签
    通过animated属性可以开启切换标签内容时的转场动画
    通过swipeable属性可以开启滑动切换标签页
    -->
    <van-tabs class="channel-tabs" v-model="active" animated swipeable>
      <van-tab
        :title="channel.channelName"
        v-for="channel in channels"
        :key="channel.channelId"
      >
        <!-- 文章列表 -->
        <article-list ref="article-list" :channel="channel" />
        <!-- /文章列表 -->
      </van-tab>
      <div slot="nav-right" class="placeholder"></div>
      <div
        slot="nav-right"
        class="hamburger-btn"
        @click="isChennelEditShow = true"
      >
        <i class="toutiao toutiao-gengduo"></i>
      </div>
    </van-tabs>
    <!-- /频道列表 -->
    <!-- 频道编辑弹出层 -->
    <van-popup
      v-model="isChennelEditShow"
      position="bottom"
      closeable
      close-icon-position="top-left"
      :style="{ height: '100%' }"
    >
      <!-- 传递channels 传递active高亮索引-->
      <channel-edit
        :my-channels="channels"
        :active="active"
        @update-active="onUpdateActive"
      />
    </van-popup>
    <!-- /频道编辑弹出层 -->
  </div>
</template>

<script>
// 1. 导入 获取频道列表的方法
import { getUserChannels } from "@/api/user";
import ArticleList from "./components/acticle-list.vue";
import ChannelEdit from "./components/channel-edit.vue";
import { mapState } from "vuex";
import { getItem } from "@/utils/storage";
import Vue from "vue";
import { Image as VanImage } from "vant";

Vue.use(VanImage);

export default {
  name: "HomeIndex",
  components: {
    ArticleList,
    ChannelEdit,
  },
  props: {},
  data() {
    return {
      // 借助这个变量来标识当前所选中的频道
      active: 0,
      channels: [{
            channelId: 0,
            channelName: "全部",
          }],
      isChennelEditShow: false, // 控制编辑频道弹出层的显示状态
    };
  },
  computed: {
    ...mapState(["user"]),
  },
  watch: {},
  created() {
    // 3. 调用获取频道列表
    this.loadChannels();
  },
  mounted() {
    window.home = this;
  },
  methods: {
    // 2. 定义加载频道列表数据的方法
    async loadChannels() {
      try {
        // const { data } = await getUserChannels()
        // this.channels = data.data.channels
        let channels = [];
        if (this.user) {
          // 已登录，请求获取用户频道列表
          const { data } = await getUserChannels(getItem("userId"));
          channels = data.data;
        } else {
          // 未登录，判断是否有本地的频道列表列表数据
          const localChannels = getItem("TOUTIAO_CHANNELS");
          // 有，拿来使用
          if (!this.user || localChannels) {
            channels = localChannels;
          }
        }

        this.channels = [...this.channels, ...channels];
        
        let temp = new Map();
        for (let item of this.channels) {
          if (!temp.has(item.channelId)) {
            temp.set(item.channelId, item);
          }
        }
        this.channels = [...temp.values()]
      } catch (err) {
        this.$toast("获取频道列表数据失败");
      }
    },
    onUpdateActive(index, isChennelEditShow = true) {
      // 更新激活的频道项
      this.active = index;
      this.isChennelEditShow = isChennelEditShow; // 控制弹出层显示与隐藏
    },
  },
};
</script>

<style scoped lang="less">
.home-container {
  padding-bottom: 100px;
  /deep/ .van-nav-bar__title {
    max-width: unset;
  }
  .search-btn {
    width: 555px;
    height: 64px;
    background-color: #ef9c7c;
    border: none;
    font-size: 28px;
    .van-icon {
      font-size: 32px;
    }
  }
}
.home-container {
  /* home-container内部的 其他已开发样式 这里省略 */
  /*添加上padding 撑开高度*/
  padding-top: 174px;

  /* 添加tabs样式 */
  /deep/ .channel-tabs {
    .van-rabs__content {
      // 响应式布局单位：vw和vh
      // vw:1vw = 布局视口宽度的1%
      // vh： 1vh = 布局视口高度的1%
      min-height: 79vh;
    }
    .van-tabs__wrap {
      height: 82px;
      position: fixed;
      top: 92px;
      left: 0;
      right: 0;
      z-index: 1;
      width: 100%;
    }
    .van-tab {
      min-width: 200px;
      border-right: 1px solid #edeff3;
      font-size: 30px;
      color: #777777;
    }

    .van-tab--active {
      color: #333333;
    }

    .van-tabs__nav {
      padding-bottom: 0;
    }

    .van-tabs__line {
      bottom: 8px;
      width: 31px !important;
      height: 6px;
      background-color: #ef9c7c;
    }
  }
}
.placeholder {
  flex-shrink: 0;
  width: 66px;
  height: 82px;
}

.hamburger-btn {
  position: fixed;
  right: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  width: 66px;
  height: 82px;
  background-color: #fff;
  opacity: 0.902;
  i.toutiao {
    font-size: 33px;
  }
  &:before {
    content: "";
    position: absolute;
    left: 0;
    width: 2px;
    height: 58px;
    background-image: url(~@/assets/gradient-gray-line.png);
    background-size: contain;
  }
}
</style>
